//
//  Listing5.m
//  SchmoozingExamples
//
//  Created by garrison on Fri Apr 20 2001.
//  Copyright (c) 2001 Standard Orbit Software, LLC. All rights reserved.
//
//  Permission is granted to use this code for any purpose, at your own risk.
//  No warranties are expressed or implied.

#import <Foundation/Foundation.h>
#import <OmniNetworking/OmniNetworking.h>

#import "Connection.h"

@implementation Connection

- initWithConnectedSocket: (ONTCPSocket*) aSocket 
{
	// Slave Step 1. Receive the accepted socket
   	self = [super init];
    	if ( self ) {
        	if ( [aSocket isConnected] )
            	mySocket = [aSocket retain];
        	else
            	return nil;
    	}
	return self;
}

- (void) processConnection 
{
	ONSocketStream *stream = nil;
	NSString *inFromClient = nil;
    
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

	stream = [ONSocketStream streamWithSocket: mySocket];
    
        NSLog(@"%@ is processing the connection", [NSThread currentThread]);
	inFromClient = [stream readLine];
	[stream writeFormat:@"%@: %@ received connection from %@\r\n", 
			[NSCalendarDate calendarDate],  
			[ONHost localHostname],
			[[mySocket remoteAddressHost] hostname]];
	[stream writeFormat:@"%@\r\n", [inFromClient uppercaseString]];
	// Slave Step 2. Interact with client.

        [mySocket abortSocket];
	// Slave Step 3. Close the connection.  This server is pretty draconian.
	NSLog(@"%@ has closed connection", [NSThread currentThread]);
    	[pool release];
}


- (void) dealloc
{
    [mySocket release];
    [super dealloc];
}
@end